**# Bookmark #1 Setting

*Windows
global root  = "G:/Dropbox/Environmental Injustice/Empirical"
global rawdata = "$root/rawdata"
global workdata = "$root/workdata"
global results = "$root/results"
global outfile = "$root/outfile"

cd "$workdata"
 
**# Bookmark #2 Table 2 Panel A: Gaps Calculation
**# Bookmark #2.1 Gaps Calculation: Cardiovascular Disease
clear
matrix T = J(6, 5, .)

use "$outfile/Individual_heartandday.dta",clear
reghdfe heart lowedu5_post_notapwater lowedu5_post lowedu5_notapwater notapwater_post lowedu5 notapwater post,noabsorb cluster(i.province i.year)

* Group 1: post=0, lowedu5=0, notapwater=0
lincom 0
local effect_1=r(estimate)

* Group 2: post=0, lowedu5=0, notapwater=1
lincom notapwater
local effect_2=r(estimate)

* Group 3: post=0, lowedu5=1, notapwater=0
lincom lowedu5
local effect_3=r(estimate)

* Group 4: post=0, lowedu5=1, notapwater=1
lincom lowedu5_notapwater + lowedu5 + notapwater
local effect_4=r(estimate)

* Group 5: post=1, lowedu5=0, notapwater=0
lincom post
local effect_5=r(estimate)

* Group 6: post=1, lowedu5=0, notapwater=1
lincom post + notapwater_post + notapwater
local effect_6=r(estimate)

* Group 7: post=1, lowedu5=1, notapwater=0
lincom post + lowedu5_post + lowedu5
local effect_7=r(estimate)

* Group 8: post=1, lowedu5=1, notapwater=1
lincom post + notapwater_post + lowedu5_post + lowedu5_post_notapwater + lowedu5_notapwater + lowedu5 + notapwater
local effect_8=r(estimate)

reghdfe heart lowedu5_post_notapwater lowedu5_post lowedu5_notapwater notapwater_post lowedu5 notapwater post,absorb(i.id) cluster(i.province i.year)

*Post-Pre Gap: without Tapwater
lincom [(post + notapwater_post + lowedu5_post + lowedu5_post_notapwater + lowedu5_notapwater + lowedu5 + notapwater) - (post + notapwater_post + notapwater)]-[(lowedu5_notapwater + lowedu5 + notapwater) - (notapwater)]
local p_1=r(p)

*Post-Pre Gap: with Tapwater
lincom [(post + lowedu5_post + lowedu5) - (post)]-[(lowedu5) - (0)]
local p_2=r(p)


matrix T[1,1] = `effect_4' - `effect_2'
matrix T[2,1] = `effect_8' - `effect_6'
matrix T[3,1] = `p_1'
matrix T[4,1] = `effect_3' - `effect_1'
matrix T[5,1] = `effect_7' - `effect_5'
matrix T[6,1] = `p_2'


**# Bookmark #2.2 Gaps Calculation: Inactive Days
use "$outfile/Individual_heartandday.dta",clear

reghdfe day lowedu5_post_notapwater lowedu5_post lowedu5_notapwater notapwater_post lowedu5 notapwater post,noabsorb cluster(i.province i.year)

* Group 1: post=0, lowedu5=0, notapwater=0
lincom 0
local effect_1=r(estimate)

* Group 2: post=0, lowedu5=0, notapwater=1
lincom notapwater
local effect_2=r(estimate)

* Group 3: post=0, lowedu5=1, notapwater=0
lincom lowedu5
local effect_3=r(estimate)

* Group 4: post=0, lowedu5=1, notapwater=1
lincom lowedu5_notapwater + lowedu5 + notapwater
local effect_4=r(estimate)

* Group 5: post=1, lowedu5=0, notapwater=0
lincom post
local effect_5=r(estimate)

* Group 6: post=1, lowedu5=0, notapwater=1
lincom post + notapwater_post + notapwater
local effect_6=r(estimate)

* Group 7: post=1, lowedu5=1, notapwater=0
lincom post + lowedu5_post + lowedu5
local effect_7=r(estimate)

* Group 8: post=1, lowedu5=1, notapwater=1
lincom post + notapwater_post + lowedu5_post + lowedu5_post_notapwater + lowedu5_notapwater + lowedu5 + notapwater
local effect_8=r(estimate)

reghdfe day lowedu5_post_notapwater lowedu5_post lowedu5_notapwater notapwater_post lowedu5 notapwater post,absorb(i.id) cluster(i.province i.year)

*Post-Pre Gap: without Tapwater
lincom [(post + notapwater_post + lowedu5_post + lowedu5_post_notapwater + lowedu5_notapwater + lowedu5 + notapwater) - (post + notapwater_post + notapwater)]-[(lowedu5_notapwater + lowedu5 + notapwater) - (notapwater)]
local p_1=r(p)

*Post-Pre Gap: with Tapwater
lincom [(post + lowedu5_post + lowedu5) - (post)]-[(lowedu5) - (0)]
local p_2=r(p)


matrix T[1,2] = `effect_4' - `effect_2'
matrix T[2,2] = `effect_8' - `effect_6'
matrix T[3,2] = `p_1'
matrix T[4,2] = `effect_3' - `effect_1'
matrix T[5,2] = `effect_7' - `effect_5'
matrix T[6,2] = `p_2'


**# Bookmark #2.3 Gaps Calculation: Tumor

use "$outfile/Individual_tumor.dta",clear
reghdfe tumor lowedu5_post_notapwater lowedu5_post lowedu5_notapwater notapwater_post lowedu5 notapwater post,noabsorb cluster(i.province i.year)

* Group 1: post=0, lowedu5=0, notapwater=0
lincom 0
local effect_1=r(estimate)

* Group 2: post=0, lowedu5=0, notapwater=1
lincom notapwater
local effect_2=r(estimate)

* Group 3: post=0, lowedu5=1, notapwater=0
lincom lowedu5
local effect_3=r(estimate)

* Group 4: post=0, lowedu5=1, notapwater=1
lincom lowedu5_notapwater + lowedu5 + notapwater
local effect_4=r(estimate)

* Group 5: post=1, lowedu5=0, notapwater=0
lincom post
local effect_5=r(estimate)

* Group 6: post=1, lowedu5=0, notapwater=1
lincom post + notapwater_post + notapwater
local effect_6=r(estimate)

* Group 7: post=1, lowedu5=1, notapwater=0
lincom post + lowedu5_post + lowedu5
local effect_7=r(estimate)

* Group 8: post=1, lowedu5=1, notapwater=1
lincom post + notapwater_post + lowedu5_post + lowedu5_post_notapwater + lowedu5_notapwater + lowedu5 + notapwater
local effect_8=r(estimate)

reghdfe tumor lowedu5_post_notapwater lowedu5_post lowedu5_notapwater notapwater_post lowedu5 notapwater post,absorb(i.id) cluster(i.province i.year)

*Post-Pre Gap: without Tapwater
lincom [(post + notapwater_post + lowedu5_post + lowedu5_post_notapwater + lowedu5_notapwater + lowedu5 + notapwater) - (post + notapwater_post + notapwater)]-[(lowedu5_notapwater + lowedu5 + notapwater) - (notapwater)]
local p_1=r(p)

*Post-Pre Gap: with Tapwater
lincom [(post + lowedu5_post + lowedu5) - (post)]-[(lowedu5) - (0)]
local p_2=r(p)


matrix T[1,3] = `effect_4' - `effect_2'
matrix T[2,3] = `effect_8' - `effect_6'
matrix T[3,3] = `p_1'
matrix T[4,3] = `effect_3' - `effect_1'
matrix T[5,3] = `effect_7' - `effect_5'
matrix T[6,3] = `p_2'

**# Bookmark #2.4 Gaps Calculation: Daily Work Hours

use "$outfile/Individual_hour.dta",clear
reghdfe hour lowedu5_post_notapwater lowedu5_post lowedu5_notapwater notapwater_post lowedu5 notapwater post,noabsorb cluster(i.province i.year)

* Group 1: post=0, lowedu5=0, notapwater=0
lincom 0
local effect_1=r(estimate)

* Group 2: post=0, lowedu5=0, notapwater=1
lincom notapwater
local effect_2=r(estimate)

* Group 3: post=0, lowedu5=1, notapwater=0
lincom lowedu5
local effect_3=r(estimate)

* Group 4: post=0, lowedu5=1, notapwater=1
lincom lowedu5_notapwater + lowedu5 + notapwater
local effect_4=r(estimate)

* Group 5: post=1, lowedu5=0, notapwater=0
lincom post
local effect_5=r(estimate)

* Group 6: post=1, lowedu5=0, notapwater=1
lincom post + notapwater_post + notapwater
local effect_6=r(estimate)

* Group 7: post=1, lowedu5=1, notapwater=0
lincom post + lowedu5_post + lowedu5
local effect_7=r(estimate)

* Group 8: post=1, lowedu5=1, notapwater=1
lincom post + notapwater_post + lowedu5_post + lowedu5_post_notapwater + lowedu5_notapwater + lowedu5 + notapwater
local effect_8=r(estimate)

reghdfe hour lowedu5_post_notapwater lowedu5_post lowedu5_notapwater notapwater_post lowedu5 notapwater post,absorb(i.id) cluster(i.province i.year)

*Post-Pre Gap: without Tapwater
lincom [(post + notapwater_post + lowedu5_post + lowedu5_post_notapwater + lowedu5_notapwater + lowedu5 + notapwater) - (post + notapwater_post + notapwater)]-[(lowedu5_notapwater + lowedu5 + notapwater) - (notapwater)]
local p_1=r(p)

*Post-Pre Gap: with Tapwater
lincom [(post + lowedu5_post + lowedu5) - (post)]-[(lowedu5) - (0)]
local p_2=r(p)

matrix T[1,4] = `effect_4' - `effect_2'
matrix T[2,4] = `effect_8' - `effect_6'
matrix T[3,4] = `p_1'
matrix T[4,4] = `effect_3' - `effect_1'
matrix T[5,4] = `effect_7' - `effect_5'
matrix T[6,4] = `p_2'

**# Bookmark #2.5 Gaps Calculation: Wage_100

use "$outfile/Individual_wage.dta",clear
reghdfe wage_100 lowedu5_post_notapwater lowedu5_post lowedu5_notapwater notapwater_post lowedu5 notapwater post,noabsorb cluster(i.province i.year)

* Group 1: post=0, lowedu5=0, notapwater=0
lincom 0
local effect_1=r(estimate)

* Group 2: post=0, lowedu5=0, notapwater=1
lincom notapwater
local effect_2=r(estimate)

* Group 3: post=0, lowedu5=1, notapwater=0
lincom lowedu5
local effect_3=r(estimate)

* Group 4: post=0, lowedu5=1, notapwater=1
lincom lowedu5_notapwater + lowedu5 + notapwater
local effect_4=r(estimate)

* Group 5: post=1, lowedu5=0, notapwater=0
lincom post
local effect_5=r(estimate)

* Group 6: post=1, lowedu5=0, notapwater=1
lincom post + notapwater_post + notapwater
local effect_6=r(estimate)

* Group 7: post=1, lowedu5=1, notapwater=0
lincom post + lowedu5_post + lowedu5
local effect_7=r(estimate)

* Group 8: post=1, lowedu5=1, notapwater=1
lincom post + notapwater_post + lowedu5_post + lowedu5_post_notapwater + lowedu5_notapwater + lowedu5 + notapwater
local effect_8=r(estimate)

reghdfe wage_100 lowedu5_post_notapwater lowedu5_post lowedu5_notapwater notapwater_post lowedu5 notapwater post,absorb(i.id) cluster(i.province i.year)

*Post-Pre Gap: without Tapwater
lincom [(post + notapwater_post + lowedu5_post + lowedu5_post_notapwater + lowedu5_notapwater + lowedu5 + notapwater) - (post + notapwater_post + notapwater)]-[(lowedu5_notapwater + lowedu5 + notapwater) - (notapwater)]
local p_1=r(p)

*Post-Pre Gap: with Tapwater
lincom [(post + lowedu5_post + lowedu5) - (post)]-[(lowedu5) - (0)]
local p_2=r(p)

matrix T[1,5] = `effect_4' - `effect_2'
matrix T[2,5] = `effect_8' - `effect_6'
matrix T[3,5] = `p_1'
matrix T[4,5] = `effect_3' - `effect_1'
matrix T[5,5] = `effect_7' - `effect_5'
matrix T[6,5] = `p_2'

**# Bookmark #2.6 Combine
matrix list T
svmat T, names(col)
keep c1 c2 c3 c4 c5
drop if c1==.
format c1 c2 c3 c4 c5 %9.3f

gen colname = ""
replace colname = "Pre-period Gaps, without Tapwater" in 1
replace colname = "Post-period Gaps, without Tapwater" in 2
replace colname = "P-value (Pre vs. Post Gaps), without Tap Water" in 3
replace colname = "Post-period Gaps, with Tapwater" in 4
replace colname = "Post-Pre Gaps, without Tapwater" in 5
replace colname = "P-value (Pre vs. Post Gaps), with Tap Water" in 6

**# Bookmark #3 Table 2 Panel B: Baseline Results

*Cardiovascular Disease
use "$outfile/Individual_heartandday.dta",clear

reghdfe heart lowedu5_post_notapwater lowedu5_post lowedu5_notapwater notapwater_post lowedu5 notapwater post ,absorb(i.id i.province#i.year) cluster(i.province i.year)

lincom lowedu5_post_notapwater + lowedu5_post

local effect = r(estimate)
local pvalue = r(p)

sum heart if e(sample)==1
local meany = r(mean)
local sdy = r(sd)

outreg2 using "$results/Table 2 Panel B.xls",replace ctitle(Cardiovascular Disease) ///
keep(lowedu5_post_notapwater lowedu5_post) ///
addtext(Individual FE, YES, ProvincebyYear FE, YES, Effects, `effect', Pvalue, `pvalue', Mean, `meany', SD, `sdy') ///
bdec(3) tdec(3) rdec(3)


*Inactive Days
use "$outfile/Individual_heartandday.dta",clear

reghdfe day lowedu5_post_notapwater lowedu5_post lowedu5_notapwater notapwater_post lowedu5 notapwater post ,absorb(i.id i.province#i.year) cluster(i.province i.year)

lincom lowedu5_post_notapwater + lowedu5_post

local effect = r(estimate)
local pvalue = r(p)

sum day if e(sample)==1
local meany = r(mean)
local sdy = r(sd)

outreg2 using "$results/Table 2 Panel B.xls",append ctitle(Inactive Days) ///
keep(lowedu5_post_notapwater lowedu5_post) ///
addtext(Individual FE, YES, ProvincebyYear FE, YES, Effects, `effect', Pvalue, `pvalue', Mean, `meany', SD, `sdy') ///
bdec(3) tdec(3) rdec(3)


*Tumor
use "$outfile/Individual_tumor.dta",clear

reghdfe tumor lowedu5_post_notapwater lowedu5_post lowedu5_notapwater notapwater_post lowedu5 notapwater post ,absorb(i.id i.province#i.year) cluster(i.province i.year)

lincom lowedu5_post_notapwater + lowedu5_post

local effect = r(estimate)
local pvalue = r(p)

sum tumor if e(sample)==1
local meany = r(mean)
local sdy = r(sd)

outreg2 using "$results/Table 2 Panel B.xls",append ctitle(Tumor) ///
keep(lowedu5_post_notapwater lowedu5_post) ///
addtext(Individual FE, YES, ProvincebyYear FE, YES, Effects, `effect', Pvalue, `pvalue', Mean, `meany', SD, `sdy') ///
bdec(3) tdec(3) rdec(3)


*Daily Work Hours

use "$outfile/Individual_hour.dta",clear

reghdfe hour lowedu5_post_notapwater lowedu5_post lowedu5_notapwater notapwater_post lowedu5 notapwater post ,absorb(i.id i.province#i.year) cluster(i.province i.year)

lincom lowedu5_post_notapwater + lowedu5_post

local effect = r(estimate)
local pvalue = r(p)

sum hour if e(sample)==1
local meany = r(mean)
local sdy = r(sd)

outreg2 using "$results/Table 2 Panel B.xls",append ctitle(Daily Work Hours) ///
keep(lowedu5_post_notapwater lowedu5_post) ///
addtext(Individual FE, YES, ProvincebyYear FE, YES, Effects, `effect', Pvalue, `pvalue', Mean, `meany', SD, `sdy') ///
bdec(3) tdec(3) rdec(3)


*Wage_100

use "$outfile/Individual_wage.dta",clear

reghdfe wage_100 lowedu5_post_notapwater lowedu5_post lowedu5_notapwater notapwater_post lowedu5 notapwater post ,absorb(i.id i.province#i.year) cluster(i.province i.year)

lincom lowedu5_post_notapwater + lowedu5_post

local effect = r(estimate)
local pvalue = r(p)

sum wage_100 if e(sample)==1
local meany = r(mean)
local sdy = r(sd)

outreg2 using "$results/Table 2 Panel B.xls",append ctitle(Wage_100) ///
keep(lowedu5_post_notapwater lowedu5_post) ///
addtext(Individual FE, YES, ProvincebyYear FE, YES, Effects, `effect', Pvalue, `pvalue', Mean, `meany', SD, `sdy') ///
bdec(3) tdec(3) rdec(3)

